﻿"use strict";
define(['admin/app', "service/admin/merchantService", "service/admin/paymentService", "service/admin/gradeService"], function (app) {
    app.controller('merchantController', function ($scope, $stateParams, $location, $state, utils, w5cValidator, $uibModal, merchantService, paymentService, gradeService) {
        $scope.query = {
            page: 1, size: 20, mobile: '', start_time: '', end_time: '', nick: '', open_wx: '', merchant_no: '', parent_id: 0, certify: 0, auth_status: '', disabled: '', is_oem: '', is_platform: '', is_supplier: '',usertype:0
        };
        $scope.auth_info = {
            page: 1, size: 20, name: '', mobile:''
        };
        $scope.oss_img_url = $scope.img_url;
        $.extend($scope.query, $stateParams);
        //alert($scope.query.page)
        var methods = {
            search: function () {
                $scope.action = 'merchant';
                if ($scope.query.is_oem == 'true') $scope.action = 'oem';
                else if ($scope.query.is_supplier == 'true') $scope.action = 'supplier';
                else if ($scope.query.is_platform == 'true') $scope.action = 'platform';
                merchantService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            search_page: function (page, i = 0) {
                if (i == 0) {
                    $scope.query.page = page;
                    $location.search('page', $scope.query.page);
                } else {
                    $scope.auth_info.page = page;
                    methods.auth_list();
                }
  
            },
            get_oems: function () {
                merchantService.getoem().success(function (response) {
                    $scope.oems = response.data;
                    $scope.oems.unshift({id:0,name:"全部"});
                });
            },
            get_oem_name:function(oem_id){
            	if(oem_id==0)return "";
            	if($scope.oems==null)return "";
            	var temp = $.grep($scope.oems, function (n, i) {
	                return n.id == oem_id;
	            });
	            if(temp.length>0)
	            	return temp[0].name;
            	return "";
            },
            edit: function () {
                $scope.action = $stateParams.action;
                if ($stateParams.id > 0) {
                    merchantService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    $scope.model = { id: 0, payments: [], setting: { hf_set: {},upgrade_set:{} } };
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    mobile: { required: '请填写账户' },
                    name: { required: '请填写名称' }
                });
            },
            auth_status_name: function (auth_status) {
                var str = '未完善';
                if (auth_status == 1) { str = '待认证' }
                else if (auth_status == 2) { str = '未通过' }
                else if (auth_status == 3) { str = '已认证' }
                return str;
            },
            auth: function () {
                merchantService.get({ id: $stateParams.id }).success(function (response) {
                    debugger;
                    $scope.model = response.data;
                    if(response.data.parent_id==0)
                    	$scope.oss_img_url=response.data.setting.oss_set.img_url;
                    else{
	                    merchantService.get({ id: response.data.parent_id }).success(function (o_response) {
	                    	$scope.oss_img_url=o_response.data.setting.oss_set.img_url;
	                	});
                	}
                });
                $scope.breadcrumb = '商户认证';
            },
            auth_list: function () {
                merchantService.auth_list($scope.auth_info).success(function (response) {
                    $scope.list = response.data;
                })
            },
            get_pay_channel: function (id) {
                var pay_channel = ["无", "恒丰", "聚维优创", "汇享天成快捷支付", "卡友快捷支付", "商付宝聚合", "睿付", "传话", "睿付代还", "汇享2", "睿付2", "XSFT", "睿付4511", "XSFT2-烟草"];
                return pay_channel[id];
                
            },
            get_status: function (id) {
                var status = ["", "不确定", "失败", "通过认证"];
                return status[id];
            },
            certifyVerifyed:function(id){
            	var modal = utils.confirm({ msg: "是否验证实名", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    merchantService.certifyVerifyed({id:id}).success(function (response) {
                		utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code != 1000)
                            $scope.model.setting.auth_info.msg=response.result_msg;
                    });
                })
            },
            merchant: function () {
                $scope.breadcrumb = '商户信息';
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    merchantService.add(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    merchantService.update(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    merchantService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            audit: function (id, auth_info, status, msg) {
                var modal = utils.confirm({ msg: status === 3 ? "确认当前用户审核通过" : "确认当前用户审核不通过", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
	            	$scope.psdisabled=true;
                    merchantService.audit({ id: id, auth_info: auth_info, status: status, msg: msg }).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else{
                        	$scope.psdisabled=false;
                        }
                    }).error(function () { 
                    	$scope.psdisabled=false;
                    });
                })
            },
            wxaudit: function (id, status, msg) {
                var modal = utils.confirm({ msg: status === 3 ? "确认当前用户微信扫码审核通过" : "确认当前用户微信扫码审核不通过", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
	            	$scope.psdisabled=true;
                    merchantService.wxaudit({ id: id, status: status, msg: msg }).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else{
                        	$scope.psdisabled=false;
                        }
                    }).error(function () { 
                    	$scope.psdisabled=false;
                    });
                })
            },
            authedit: function (id, auth_info) {
                var modal = utils.confirm({ msg: "确认修改当前用户认证内容", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
	            	$scope.psdisabled=true;
                    merchantService.authedit({ id: id, auth_info: auth_info }).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
	                	$scope.psdisabled=false;
                    }).error(function () { 
                    	$scope.psdisabled=false;
                    });
                })
            },
            is_register:function(payment,settle_type,registers){
            	if(payment.is_open && payment.is_register && !payment.is_more){
            		var temp = $.grep(registers, function (n, i) {
		                return n.pay_channel == payment.channel && n.settle_type == settle_type;
		            });
        			return temp.length==0;
        		}
            	return false;
            },
            add_merchant: function (id, pay_channel,settle_type,account_no) {
            	$scope.psdisabled=true;
                merchantService.addmerchant({ id: id, pay_channel:pay_channel,settle_type:settle_type,account_no:account_no}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000){
                    	methods.edit();
                    }
                	$scope.psdisabled=false;
                }).error(function () { 
                	$scope.psdisabled=false;
                });
            },
            auth_register: function (id, pay_channel,settle_type,op) {
            	$scope.psdisabled=true;
                merchantService.auth_register({ id: id, pay_channel:pay_channel,settle_type:settle_type,op:op}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000){
                    	methods.edit();
                    }
                	$scope.psdisabled=false;
                }).error(function () { 
                	$scope.psdisabled=false;
                });
            },
            open_payment: function () {
                $uibModal.open({
                    templateUrl: '/tpl/payment.html',
                    controller: 'payment_Ctrl',
                    resolve: {
                        mode: function () {
                            return { payments: $scope.model.payments };
                        }
                    }
                }).result.then(function (payments) {
                    $scope.model.payments = payments;
                });
            },
            auth_register_del:function(id){
            	$scope.psdisabled=true;
                merchantService.auth_register_del({ id: id}).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000){
                    	methods.edit();
                    }
                	$scope.psdisabled=false;
                }).error(function () { 
                	$scope.psdisabled=false;
                });
            },
            get_grade: function (merchant_no) {
                if ($scope.model == null) $scope.model = {};
                $scope.model.merchant_no = merchant_no;
                gradeService.gets({ id: merchant_no }).success(function (response) {
                    $scope.grades = response.data;
                });
            },
            edit_grade: function () {
                if ($stateParams.id > 0) {
                    gradeService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    $scope.model = { id: 0, merchant_no: $stateParams.merchant_no, setting: { payments: [] } };
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    name: { required: '请填写名称' },
                    grade_no: { required: '请填写编号' }
                });
            },
            save_grade: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    gradeService.post(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    gradeService.put(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            }
        }

        angular.extend($scope, methods);
        if ($state.includes('app.merchant.detail'))
            methods.edit();
        else if ($state.includes('app.merchant.auth'))
            methods.auth();
        else if ($state.includes('app.merchant.merchant'))
            methods.merchant();
        else if ($state.includes('app.merchant.grade'))
            methods.get_grade($stateParams.id);
        else if ($state.includes('app.merchant.grade_detail'))
            methods.edit_grade();
        else if ($state.includes('app.merchant.auth_list'))
            methods.auth_list();
        else {
            methods.search();
            if ($stateParams.is_oem == 'false' || $stateParams.is_supplier == 'true') {
                methods.get_oems();
            }
        }
    });
    app.controller('payment_Ctrl', function ($scope, $uibModalInstance, utils, paymentService, mode) {
        $scope.payments = [];
        $.each(mode.payments, function (i, item) { $scope.payments.push(item); });

        $scope.ok = function () {
            $uibModalInstance.close($scope.payments);
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

        $scope.choose_payment = function (item) {
            var temp = $.grep($scope.payments, function (n, i) {
                return n.payment_id == item.id;
            });
            if (temp.length > 0) {
                for (var i = 0; i < temp.length; i++) {
                    var index = $.inArray(temp[i], $scope.payments);
                    $scope.payments.splice(index, 1);
                }
            }
            else {
                var settles = item.settles;
                $.each(settles, function (i, settle) { settle.fee = ''; });
                var payement = { payment_id: item.id, settles: settles, payment: item };
                $scope.payments.push(payement);
            }
        };

        $scope.is_choose = function (id) {
            var temp = $.grep($scope.payments, function (n, i) {
                return n.payment_id == id;
            });
            return temp.length > 0;
        }

        $scope.search = function () {
            paymentService.gets().success(function (response) {
                $scope.list = response.data;
            });
        }

        $scope.search();
    });
});